package nocuffin.m5;



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.FileOutputStream;



import android.content.Context;

import android.os.Environment;




/** 
 * Team: No Cuffin'
 * 2/27/2012
 * 
 *
 * This will be our MainUI!!
 * 
 * 
 * @author Robert Allen, Philip Johnson, Steven Moore
 * @version 1.0 2/27/2012
 *
 */ 
public class M5Activity extends Activity{
	Button bLogin, bNew, bTask;
	TextView label, invalidpassword;
	EditText usernametext, passwordtext;
	String collected;
	@Override
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.main);
	     bLogin = (Button) findViewById(R.id.loginButton);
	     bNew = (Button) findViewById(R.id.createnewaccountButton);
	     bTask = (Button) findViewById(R.id.taskButton);
	     invalidpassword = (TextView) findViewById(R.id.invalidpasswordLabel);
	     label = (TextView) findViewById (R.id.invalidpasswordLabel);
	     passwordtext = (EditText) findViewById (R.id.passwordText);
	     usernametext = (EditText) findViewById (R.id.usernameText);
	     bNew.setOnClickListener(new View.OnClickListener() {
	        	
	    		public void onClick(View v) {
	    			Class ourClass;
					try {
						ourClass = Class.forName("nocuffin.m5." + "NewUserScreen");
						Intent myIntent = new Intent(v.getContext(), ourClass);
	    		 	     //This is our username...which we must pass continuously
						
	    		        //Add the set of extended data to the intent and start it
	    		        //Add the set of extended data to the intent and start it
						startActivity(myIntent);
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	    			
	    			
	    		}
	     });
	     bLogin.setOnClickListener(new View.OnClickListener() {
	        	
	    		public void onClick(View v) {
	    			String login = usernametext.getText().toString() + passwordtext.getText().toString();
	    			FileInputStream fis;
	    			
	    			
	    			try 
					{
						fis = openFileInput("key");
						byte[] dataArray = new byte[fis.available()];
						while (fis.read(dataArray) != -1)
						{
							collected = new String(dataArray);
						}
					
					} 
					catch (FileNotFoundException e) 
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 	
					catch (IOException e) 
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	    			
	    			if (collected.contains(login + "\n") || collected.contains(login + ":"))
		    		{
		    			Class ourClass;
						try {
							ourClass = Class.forName("nocuffin.m5." + "MainUI");
							Intent myIntent = new Intent(v.getContext(), ourClass);
							Bundle d = new Bundle(); 
		    		 	     //This is our username...which we must pass continuously
							String name = login;
		    		        d.putString("name", name);
		    		        //Add the set of extended data to the intent and start it
		    		        myIntent.putExtras(d);
		    		        //Add the set of extended data to the intent and start it
							startActivity(myIntent);
						} catch (ClassNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
		    		}
	    			
	    			else
	    			{
	    				invalidpassword.setText("Invalid username/password combination.");
	    			}
	    		
	    		}
	    		
	     });
	}
}
	     
	
	
